﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using FileSystem.Models;

namespace FileSystem.Services.IServices {
    /// <summary>
    /// 磁盘块存储接口
    /// </summary>
    public interface IDiskBlockStorage {
        /// <summary>
        /// 初始化磁盘块存储
        /// </summary>
        Task InitializeAsync();

        /// <summary>
        /// 磁盘块存储是否已经初始化
        /// </summary>
        bool IsInitialized();

        /// <summary>
        /// 增加磁盘块
        /// </summary>
        /// <param name="diskBlock">要添加的磁盘块</param>
        Task AddAsync(DiskBlock diskBlock);

        /// <summary>
        /// 删除磁盘块
        /// </summary>
        /// <param name="diskBlock">要删除的磁盘块</param>
        Task RemoveAsync(DiskBlock diskBlock);

        /// <summary>
        /// 修改磁盘块属性
        /// </summary>
        /// <param name="diskBlock">磁盘块</param>
        /// <param name="property">属性</param>
        /// <param name="newContent">新值</param>
        Task ModifyProperty(DiskBlock diskBlock,
            DiskBlockConst.Property property, int newContent);

        /// <summary>
        /// 根据物理块号获得一个磁盘块
        /// </summary>
        /// <param name="physicalBlockNumber">物理块号</param>
        Task<DiskBlock> GetDiskBlock(int physicalBlockNumber);

        /// <summary>
        /// 得到第一个空磁盘块
        /// </summary>
        Task<DiskBlock> GetFirstNullDiskBlock();

        /// <summary>
        /// 得到最后一个空磁盘块
        /// </summary>
        Task<DiskBlock> GetLastNullDiskBlock(); 

        /// <summary>
        /// 获得所有磁盘块
        /// </summary>
        Task<IList<DiskBlock>> GetDiskBlocks();

        /// <summary>
        /// 判断磁盘块是否存在
        /// </summary>
        /// <param name="physicalBlockNumber">物理块号</param>
        Task<bool> Exist(int physicalBlockNumber);

        /// <summary>
        /// 判断磁盘块是否为空
        /// </summary>
        /// <param name="physicalBlockNumber">物理块号</param>
        Task<bool> IsNull(int physicalBlockNumber);
    }

    /// <summary>
    /// 磁盘块存储常量
    /// </summary>
    public static class DiskBlockStorageConstants
    {
        /// <summary>
        /// 默认的磁盘块数据库版本号。
        /// </summary>
        public const int DefaultVersion = 0;

        /// <summary>
        /// 磁盘块数据库版本号。
        /// </summary>
        public const int Version = 1;

        /// <summary>
        /// 磁盘块数据库版本号键。
        /// </summary>
        public const string VersionKey = nameof(DiskBlockStorageConstants) +
            "." + nameof(Version);
    }
}